package com.example.myanimator.one;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * @author tgw
 * @date 2021/3/15
 * @describe
 */
class DrawPressPoint extends View implements View.OnTouchListener {

    private Paint mPaint;
    private float pointX = -1,pointY=-1;
    private RectF pointRect;

    public DrawPressPoint(Context context) {
        super(context);
    }

    public DrawPressPoint(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        mPaint.setColor(Color.parseColor("#0BF669"));
        mPaint.setStyle(Paint.Style.FILL);
        pointRect = new RectF(0,0,100,100);
//        setOnTouchListener(this);
    }



    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(100, 100);
        setMeasuredDimension(100, 100);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        if (pointRect.contains(pointX,pointY)){
            mPaint.setColor(Color.parseColor("#dBD369"));
        }else {
            mPaint.setColor(Color.parseColor("#0BF669"));
        }
        canvas.drawRect(pointRect,mPaint);

        boolean isIntersects=pointRect.intersects(80,80,100,100);
        Log.d("TGW是否相交intersects：如果相交返回结果",isIntersects+"--"+pointRect.toString());
        boolean isIntersect=pointRect.intersect(new RectF(80,80,100,100));
        Log.d("TGW是否相交intersect：如果相交返回相交后的结果--区域",isIntersect+"--"+pointRect.toString());
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        pointX = event.getX();
        pointY = event.getY();
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                postInvalidate();
                return true;//返回true 代表当前view消费该事件  ACTION_UP 等事件后续会传过来
            case MotionEvent.ACTION_UP:
                pointX = -1;
                pointY=-1;
                break;
        }
        /**前者要在主线程调用，后者哪里都可以调用 */
//        invalidate();
        postInvalidate();
        return super.onTouchEvent(event);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        return super.dispatchTouchEvent(event);
    }

    /**要想该事件响应 必须设置 注册在该view的构造函数中 setOnTouchListener(this);
     *  另外分发顺序为 dispatchTouchEvent 》 onTouch（这个需要注册-如果返回true 则不会响应，onTouchEvent） 》onTouchEvent
     */
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return false;
    }
}
